package com.wbao.dianniu.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.listener.IFriendRemarkListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.FriendRemarkManager;
import com.wbao.dianniu.manager.ModifyRemarkHelper;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.R;

/**
 * 修改备注
 * 
 * @author yejianjun
 *
 */
public class ModifyRemarkActivity extends BaseActivity implements IFriendRemarkListener {

	private EditText mEditText;
	private ImageButton deletedBtn;
	private Button rightBtn;
	private FriendRemarkManager manager;
	private int friendAccountId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentLayout(R.layout.modify_remark);
		setTitleName("修改备注");
		rightBtn = getRightTextButton();
		rightBtn.setText("确认");
		rightBtn.setOnClickListener(this);
		mEditText = (EditText) findViewById(R.id.remarkEdit);
		deletedBtn = (ImageButton) findViewById(R.id.remarkDelete);
		deletedBtn.setOnClickListener(this);
		manager = new FriendRemarkManager(this);
		manager.addFriendRemarkListener(this);
		friendAccountId = getIntent().getIntExtra(GlobalContext.BUNDLE_ACCOUNTID, 0);
		String remark = getIntent().getStringExtra(GlobalContext.BUNDLE_REALNAME);
		mEditText.setText(remark == null ? "" : remark);
	}
	
	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.remarkDelete:
			mEditText.setText("");
			break;
		case R.id.right_button:
			String name = mEditText.getText().toString().trim();
			if (TextUtils.isEmpty(name)) {
				Notification.toast(ModifyRemarkActivity.this, "昵称不能为空");
				return;
			}
			manager.friendRemark(GlobalContext.accountId, friendAccountId, mEditText.getText().toString());
			break;
		default:
			break;
		}
		
	}
	
	@Override
	public void onFriendRemarkFailure(int errorCode, String errorMsg) {
	}

	@Override
	public void onFriendRemarkSuccess() {
		ModifyRemarkHelper.getInstance().remarkChanged(mEditText.getText().toString());
		finish();
	}
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		}
		public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		}
}
